package com.example.views;

import java.util.ArrayList;

import com.example.R;
import com.example.interfaces.IPostItemActivity;
import com.example.model.Date;
import com.example.model.User;
import com.example.presenters.PostItemPresenter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * This activity will be the post item view for the user. It will take the user
 * input and send it to the presenter, so it can create the item and post it to
 * the ItemList.
 * 
 * @author 3Factorial
 */
public class PostItemActivity extends Activity implements IPostItemActivity {

	private PostItemPresenter myPresenter;
	private String currUsername;

	/**
	 * Activates the register activity when it is called.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_post_item);
		myPresenter = new PostItemPresenter(this,
				com.example.model.UserList.getInstance(),
				com.example.model.ItemList.getInstance());
		Intent intent = getIntent();
		currUsername = intent.getStringExtra(MainActivity.USERNAME);
	}

	/**
	 * Builds up the options menu.
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * Cancels the item creating procedure and sends the user back to the
	 * profile activity.
	 * 
	 * @param view
	 *            The profile activity.
	 */
	public void cancel(View view) {
		Intent intent = new Intent(this, ProfileActivity.class);
		intent.putExtra(MainActivity.USERNAME, currUsername);
		startActivity(intent);
	}

	/**
	 * Sends the item information to the presenter so it can create the item.
	 * 
	 * @param view
	 */
	public void postItem(View view) {
		String itemName = getItemName();
		String description = getDescription();
		String loc = getLoc();
		Date date = getDate();
		int lost = getLostStatus();
		if(itemName == null || description == null || loc == null){
			StringBuilder errorString = new StringBuilder("Missing required field(s):");
			if (itemName == null)
				errorString.append("\nitem name");
			if (loc == null)
				errorString.append("\nlocation");
			if (description == null)
				errorString.append("\ndescription");
			TextView errorMsg = (TextView) findViewById(R.id.textView_error_msg);
			errorMsg.setText(errorString.toString());
		}else{
		myPresenter.addItem(itemName, description, loc, currUsername, date,
				lost); // lost should get changed with checkStatus()
		}

	}

	/**
	 * Sends the user to an item's screen.
	 * 
	 * @param itemName
	 *            The name of item to view.
	 */
	public void goToItemScreen(String itemName) {
		Intent intent = new Intent(this, ItemActivity.class);
		intent.putExtra(MainActivity.ITEMNAME, itemName);
		startActivity(intent);
	}

	/**
	 * Sends the user back to the user profile.
	 */
	public void goToProfile() {
		Intent intent = new Intent(this, ProfileActivity.class);
		intent.putExtra(MainActivity.USERNAME, currUsername);
		startActivity(intent);
	}

	/**
	 * @return The item's name.
	 */
	private String getItemName() {
		String toReturn = ((EditText) findViewById(R.id.editText_item_name)).getText()
				.toString();
		if (toReturn.isEmpty())
			return null;
		else
			return toReturn;
	}

	/**
	 * @return The item's description.
	 */
	private String getDescription() {
		String toReturn = ((EditText) findViewById(R.id.editText_description)).getText()
				.toString();
		if (toReturn.isEmpty())
			return null;
		else
			return toReturn;
	}

	/**
	 * @return The item's location.
	 */
	private String getLoc() {
		String toReturn = ((EditText) findViewById(R.id.editText_location_found))
				.getText().toString();
		if (toReturn.isEmpty())
			return null;
		else
			return toReturn;
	}

	/**
	 * Retrieves the date picker from the form and creates a valid date from the
	 * picker's information.
	 * 
	 * @return The date described by the date picker.
	 */
	private Date getDate() {
		DatePicker dp = (DatePicker) findViewById(R.id.datePicker1);
		int day = dp.getDayOfMonth();
		int month = dp.getMonth();
		int year = dp.getYear();
		Date d = new Date(day, month, year);
		return d;
	}

	/**
	 * Determines what classification the item will be given from the form. It
	 * can be described as 1) lost, 2) found, or 3) donated.
	 * 
	 * @return The item's desired classification.
	 */
	public int getLostStatus() {
		RadioGroup status = (RadioGroup) findViewById(R.id.radioGroup1);
		int s = status.getCheckedRadioButtonId();
		int toReturn = 0;
		// TODO Don't we need breaks here? - Armo
		switch (s) {
		case R.id.lost:
			toReturn = 0;
		case R.id.found:
			toReturn = 1;
		case R.id.donated:
			toReturn = 2;
		}
		return toReturn;
	}

	// Javadoc from interface
	public ArrayList<String> getTags() {

		// TODO Make this more efficient. Let's face it, I did terrible coding
		// here...
		// - Armando
		ArrayList<String> possibleTags = new ArrayList<String>();

		CheckBox clothesBox = (CheckBox) findViewById(R.id.clothes_checkbox);
		CheckBox electronicsBox = (CheckBox) findViewById(R.id.electronics_checkbox);
		CheckBox equipmentBox = (CheckBox) findViewById(R.id.equipment_checkbox);
		CheckBox furnitureBox = (CheckBox) findViewById(R.id.furniture_checkbox);
		CheckBox housewaresBox = (CheckBox) findViewById(R.id.housewares_checkbox);
		CheckBox hygieneBox = (CheckBox) findViewById(R.id.hygiene_checkbox);
		CheckBox petsBox = (CheckBox) findViewById(R.id.pets_checkbox);
		CheckBox toysBox = (CheckBox) findViewById(R.id.toys_checkbox);
		CheckBox otherBox = (CheckBox) findViewById(R.id.other_checkbox);

		if (clothesBox.isChecked())
			possibleTags.add("Clothes");

		if (electronicsBox.isChecked())
			possibleTags.add("Electronics");

		if (equipmentBox.isChecked())
			possibleTags.add("Equipment");

		if (furnitureBox.isChecked())
			possibleTags.add("Furniture");

		if (housewaresBox.isChecked())
			possibleTags.add("Housewares");

		if (hygieneBox.isChecked())
			possibleTags.add("Hygiene");

		if (petsBox.isChecked())
			possibleTags.add("Pets");

		if (toysBox.isChecked())
			possibleTags.add("Toys");

		if (otherBox.isChecked())
			possibleTags.add("Other");

		return possibleTags;
	}
}
